#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <fstream>
#include "Log.hpp"

const std::string mydict = "./recource/dict.txt";
const std::string sep = " ";
const std::string Unknow = "Unknow";

class Translate
{
public:
    Translate(std::string path = mydict)
        : _dict_path(path)
    {
        // 1,加载单词组
        LoadDict();
        // 2.切割单词组
        cuttword();
    }
    
    void LoadDict()
    {
        std::ifstream in(_dict_path);
        std::string line;
        while (std::getline(in, line))
        {
            _wordgroups.push_back(line);
        }
        lg(Debug, "LoadDict dict txt success , path : %s\n wordsize : %", _dict_path.c_str());
    }

    void cuttword()
    {
        for (auto &str : _wordgroups)
        {
            int pos = str.find(sep);
            if (pos == std::string::npos)
                continue;
            std::string word = str.substr(0, pos);
            size_t StartPos = pos + 1;
            // 去掉空格
            while (str[StartPos] == ' ' && StartPos < str.size())
                StartPos++;

            std::string chinese = str.substr(StartPos);
            _dict[word] = chinese;
        }
        lg(Debug, "cuttword dict txt success , path : %s\n", _dict_path.c_str());
    }


    size_t WordSize()
    {
        return _wordgroups.size();
    }
    std::string Excute(std::string _key)
    {
        if(_dict.find(_key) == _dict.end())
        {
            return Unknow;
        }
        return _dict[_key];
    }
    
    ~Translate()
    {
    }

private:
    std::string _dict_path;
    std::vector<std::string> _wordgroups;
    std::unordered_map<std::string, std::string> _dict;
};